<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        修改供应商
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../css/main.css" media="all">
    <script type="text/javascript" src="../../../../lib/loading/okLoading.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../../../../lib/layui/css/layui_2.5.7.css">

</head>
<style>
    .layui-form-label {
        width: 150px !important;
    }

    .wauto {
        width: auto;
    }
</style>

<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                        style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body" id="app">
    <form class="layui-form layui-form-pane" id="add">
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <div class="layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>名称</label>
                    <div style="width: 72%" class="layui-input-inline">
                        <input  autocomplete="off"  disabled type="text" name="supplierName"
                               lay-verify="required" v-model="supplierData.supplierName" placeholder="名称"
                               class="layui-input">
                    </div>
                </div>
                <h5><b>联系方式</b></h5><br>
                <div class="layui-inline layui-form-item">
                    <label class="layui-form-label" id="hkje"> <span class='x-red'>*</span>联系人员</label>
                    <div class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="contactDataPerson"
                               lay-verify="required" v-model="supplierDataPerson"
                               placeholder="联系人员" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>联系方式</label>
                    <div class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="contactDataPhone"
                               lay-verify="required"
                               v-model="supplierDataPhone"
                               placeholder="联系方式" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>渠道</label>
                    <div style="width: 72%" class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="channel"
                               lay-verify="required" v-model="supplierData.channel" placeholder="渠道"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>产品涵盖</label>
                    <div style="width: 72%" class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="productCover"
                               lay-verify="required" v-model="supplierData.productCover" placeholder="产品涵盖"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>适用电梯</label>
                    <div style="width: 72%" class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="appiyElevator"
                               lay-verify="required" v-model="supplierData.appiyElevator" placeholder="适用电梯"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"> <span class='x-red'>*</span>省市地区</label>
                    <div style="width: 23%" class="layui-input-inline">
                        <select ay-verify="required" lay-filter="addressProvince" name="addressProvince">
                            <option value="">---</option>
                        </select>
                    </div>
                    <div style="width: 24%" class="layui-input-inline">
                        <select ay-verify="required" lay-filter="addressCity" name="addressCity">
                            <option value="">---</option>
                        </select>
                    </div>
                    <div style="width: 25%" class="layui-input-inline">
                        <select ay-verify="required" lay-filter="addressArea" name="addressArea">
                            <option value="">---</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详细地址</label>
                    <div style="width: 72%" class="layui-input-inline">
                        <input  autocomplete="off"  type="text" name="addressmsg" v-model="supplierData.addressmsg" placeholder="详细地址"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注信息</label>
                    <div style="width: 70%" class="layui-input-inline">
                         <textarea v-model="supplierData.supplierRemark" name="supplierRemark" placeholder="请输入内容"
                                   class="layui-textarea"></textarea>
                    </div>
                </div>


                <div style="text-align: center" class="layui-form-item">
                    <button class="layui-btn" type="button" lay-filter="sub" lay-submit="sub">
                        保存
                    </button>
                </div>
                <div style="height:20px;"></div>
            </div>
        </div>
    </form>
    <h5><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;配件报价</b></h5><br>
    <button @click="librarySelection" class="btn btn-info btn-xs" style="width: auto;float: right;margin-right: 3%">
        <span class="glyphicon glyphicon-plus" aria-hidden="true">从库中选</span>
    </button>
    <div style="clear: both;"></div>
    <div class="layui-card-body">
        <div class="table-responsive">
            <table style="text-align: center" class="table table-bordered">
                <thead>
                <tr class="active">
                    <td>配件名称</td>
                    <td>配件规格</td>
                    <td>物料号</td>
                    <td>供应商料号</td>
                    <td>配件报价/元</td>
                    <td>备注</td>
                    <td style="width: 11%">操作</td>
                </tr>
                </thead>

                <tbody>
                <tr v-for="(item,index) in partsOfferList">
                    <td>{{item.partsName}}</td>
                    <td>{{item.specification}}</td>
                    <td>{{item.partsNum}}</td>
                    <td>{{item.supplierNum}}</td>
                    <td>{{item.partsOffer}}</td>
                    <td>{{item.offerRemark}}</td>
                    <td>
                        <button @click="edit(item,index)" class="btn btn-primary btn-xs wauto">
                            <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
                        </button>
                        <button @click="del(index)" class="btn btn-danger btn-xs wauto">
                            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                        </button>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <br><br><br><br><br><br>
</div>
<script type="text/javascript" src="../../../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../../../js/myAjax.js"></script>
<script type="text/javascript" src="../../../../js/pub.js"></script>
<script src="../../../../lib/layui/layui2.5.7.js" charset="utf-8"></script>
<script src="../../../../js/x-layui.js" charset="utf-8"></script>
<script src="../../../../js/vue.min.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script>
    layui.use(['element', 'layer', 'form', 'laydate'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        laydate = layui.laydate;//日期插件
        form = layui.form;
        okLoading.close($);
    })
</script>
<script>
    let recordId = getQueryVariable("recordId");


    var vm = new Vue({
        el: "#app",
        data: {
            recordId,
            supplierData: null,
            // suffixLength: null,
            supplierDataPerson: null,
            supplierDataPhone: null,
            partsOfferList: null
        },
        mounted() {
            let request = {
                url: 'kf/supplier/getSupplierMsgById',
                data: {
                    userId: $userId,
                    supplierId: recordId,
                    page: 1,
                    size: 9999
                },
                method: "POST",
            };
            sendRequest(request, (res) => {
                this.supplierData = res.data.SupplierData[0];
                this.supplierDataPerson = this.supplierData.contact.split('|')[0].substring(1);
                this.supplierDataPhone = this.supplierData.contact.split('|')[1].substring(0, this.supplierData.contact.split('|')[1].length - 1);
            });


            //配件报价
            let request1 = {
                url: 'kf/supplier/getPartsOffer',
                data: {
                    userId: $userId,
                    supplierId: recordId,
                    size: 99999,
                    page: 1
                },
                method: "get",
            };
            sendRequest(request1, (res) => {
                this.partsOfferList = res.data.PartsOfferData;
                localStorage.setItem("selected_parts_offers", JSON.stringify(this.partsOfferList || []));
            });
        },
        methods: {
            edit(item, index) {
                localStorage.setItem("edit_parts_offer", JSON.stringify(item));
                layer.open({
                    title: '请选择',
                    type: 2,
                    btnAlign: 'c',
                    area: ['80%', '80%'],
                    content: 'supplier_parts_price_edit.html',
                    btn: ['确定', '关闭'],
                    yes: (i, layero) => {

                        this.partsOfferList.splice(index, 1, JSON.parse(localStorage.getItem("edit_parts_offer")))

                        layer.close(i);
                    }
                });

            },
            del(index) {
                this.partsOfferList.splice(index, 1);
                localStorage.setItem("selected_parts_offers", JSON.stringify(this.partsOfferList));
            },
            librarySelection() {

                layer.open({
                    title: '选择配件',
                    type: 2,
                    btnAlign: 'c',
                    area: ['80%', '80%'],
                    content: 'inventory_query.html',
                    btn: ['确定', '关闭'],
                    yes: (i, layero) => {

                        this.partsOfferList = JSON.parse(localStorage.getItem("selected_parts_offers"));

                        layer.close(i);
                    }
                });

            }
        }

    });


    layui.use(['laydate', 'form', 'layer', 'table'],
        function () {
            var form = layui.form;
            var layer = layui.layer;
            //监听审批提交
            form.on('submit(sub)',
                function (data) {
                    let form = data.field;
                    form.userId = $userId;
                    form.operationType = 'edit';
                    form.id = vm.supplierData.id;
                    form.contactData = '{' + form.contactDataPerson + '|' + form.contactDataPhone + '}';


                    let t = vm.partsOfferList;
                    if(t === undefined){
                        t = [];
                    }
                    $.each(t, (index, item) => {
                        item.supplierId = vm.supplierData.id;
                    });
                    //保存供应商报价信息
                    let request = {
                        url: 'kf/supplier/addPartsOffer',
                        data: {
                            userId: $userId,
                            kfPartsOfferList: JSON.stringify(t)
                        },
                        method: "post",
                    };
                    sendRequest(request, (data) => {
                        if (data.code === 0) {


                            //保存供应商信息
                            let request2 = {
                                url: 'kf/supplier/addSupplier',
                                data: form,
                                method: "post",
                            };
                            sendRequest(request2, (data) => {
                                console.log(data);
                                if (data.code === 0) {
                                    layer.alert("操作成功", {
                                            icon: 6
                                        },
                                        function () {
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(index);
                                            parent.reload();

                                        });
                                } else {
                                    layer.alert(data.msg, {
                                        icon: 5
                                    })
                                }
                            });


                        } else {
                            layer.alert(data.msg, {
                                icon: 5
                            })
                        }
                    });


                    return false;
                });

            form.on('select(addressProvince)', function (data) {
                $("select[name=addressCity]").empty();
                $("select[name=addressArea]").empty();
                let request = {
                    url: '/other/region/findRegionByParentId',
                    method: 'get',
                    data: {
                        userId: $userId,
                        parentId: $("select[name=addressProvince]").val()
                    }
                };
                sendRequest(request, res => {
                    let x = res.data;
                    $.each(x, function (index, item) {
                        let opt = $("<option value=" + item.id + ">" + item.name + "</option>");
                        $("select[name=addressCity]").append(opt);
                    });
                    form.render('select');
                    $("select[name=addressArea]").empty();
                    changeRegion('addressCity', 'addressArea');
                })
            });

            form.on('select(addressCity)', function (data) {
                $("select[name=addressArea]").empty();
                changeRegion('addressCity', 'addressArea');
            });
        });


    function changeRegion(parentName, selectName) {
        let request = {
            url: '/other/region/findRegionByParentId',
            method: 'get',
            data: {
                userId: $userId,
                parentId: $("select[name=" + parentName + "]").val()
            }
        };
        sendRequest(request, res => {
            let x = res.data;
            $.each(x, function (index, item) {
                let opt = $("<option value=" + item.id + ">" + item.name + "</option>");
                $("select[name=" + selectName + "]").append(opt);
            });

            form.render('select');
        })
    }


    //省市地区
    changeRegion('null', 'addressProvince');

    setTimeout(() => {
        f1();
    }, 100);
    setTimeout(() => {
        f1();
    }, 700);
    setTimeout(() => {
        f1();
    }, 1400);

    function f1() {

        layui.use(['form'],
            function () {
                var form = layui.form;
                $("select[name=addressProvince]").val(vm.supplierData.addressProvince);
                form.render('select');

                let request = {
                    url: '/other/region/findRegionByParentId',
                    method: 'get',
                    data: {
                        userId: $userId,
                        parentId: vm.supplierData.addressProvince
                    }
                };
                sendRequest(request, res => {
                    let x = res.data;
                    $.each(x, function (index, item) {
                        let opt = $("<option value=" + item.id + ">" + item.name + "</option>");
                        $("select[name=addressCity]").append(opt);
                    });

                    $("select[name=addressCity]").val(vm.supplierData.addressCity);
                    form.render('select');


                    let request = {
                        url: '/other/region/findRegionByParentId',
                        method: 'get',
                        data: {
                            userId: $userId,
                            parentId: vm.supplierData.addressCity
                        }
                    };
                    sendRequest(request, res => {
                        let x = res.data;
                        $.each(x, function (index, item) {
                            let opt = $("<option value=" + item.id + ">" + item.name + "</option>");
                            $("select[name=addressArea]").append(opt);
                        });
                        $("select[name=addressArea]").val(vm.supplierData.addressArea);
                        form.render('select');
                    })

                })
            })
    }
</script>

</body>

</html>
